﻿using Cinch;
using FR.SAFTAnalyser.Services;
using MEFedMVVM.Common;
using MEFedMVVM.ViewModelLocator;
using System.ComponentModel.Composition;
using System.Deployment.Application;
using System.Reflection;

namespace FR.SAFTAnalyser.ViewModels
{
    [ExportViewModel("AboutViewModel")]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class AboutViewModel : ViewModelBase
    {
        public AboutViewModel()
        {
            if (!Designer.IsInDesignMode)
            {
                this.DisplayName = "Sobre";

                Mediator.Instance.Register(this);

                DoOpenMainWebSiteCommand = new SimpleCommand<object, object>(CanOpenMainWebSite, OpenMainWebSite);
                DoOpenDocumentacaoCommand = new SimpleCommand<object, object>(CanOpenDocumentacao, OpenDocumentacao);
                DoOpenCertificacaoCommand = new SimpleCommand<object, object>(CanOpenCertificacao, OpenCertificacao);
                DoOpenPortalFinancasCommand = new SimpleCommand<object, object>(CanOpenPortalFinancas, OpenPortalFinancas);

                if (ApplicationDeployment.IsNetworkDeployed)
                {
                    ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment;

                    //actualizar o texto com a versão
                    this.Version = ad.CurrentVersion.ToString();
                }
                else
                    this.Version = "Offline";
            }
        }

        #region Binding Properties
        public string MainWebSite
        {
            get { return HyperlinkService.MainWebSite; }
            set
            {
                NotifyPropertyChanged(ObservableHelper.CreateArgs<AboutViewModel>(x => x.MainWebSite));
            }
        }

        public string Documentacao
        {
            get { return HyperlinkService.Documentacao; }
            set
            {
                NotifyPropertyChanged(ObservableHelper.CreateArgs<AboutViewModel>(x => x.Documentacao));
            }
        }

        public string Certificacao
        {
            get { return HyperlinkService.Certificacao; }
            set
            {
                NotifyPropertyChanged(ObservableHelper.CreateArgs<AboutViewModel>(x => x.Certificacao));
            }
        }

        public string PortalFinancas
        {
            get { return HyperlinkService.PortalFinancas; }
            set
            {
                NotifyPropertyChanged(ObservableHelper.CreateArgs<AboutViewModel>(x => x.PortalFinancas));
            }
        }

        string version;
        public string Version
        {
            get { return version; }
            set
            {
                version = value;
                NotifyPropertyChanged(ObservableHelper.CreateArgs<AboutViewModel>(x => x.Version));
            }
        }

        bool isCheckingForUpdate;
        public bool IsCheckingForUpdate
        {
            get { return isCheckingForUpdate; }
            set
            {
                isCheckingForUpdate = value;
                NotifyPropertyChanged(ObservableHelper.CreateArgs<AboutViewModel>(x => x.IsCheckingForUpdate));
            }
        }

        string updateStatus;
        public string UpdateStatus
        {
            get { return updateStatus; }
            set
            {
                updateStatus = value;
                NotifyPropertyChanged(ObservableHelper.CreateArgs<AboutViewModel>(x => x.UpdateStatus));
            }
        }
        #endregion Binding Properties

        #region Message mediator

        [MediatorMessageSink(MessageType.UPDATE_STATUS)]
        private void UpdateStatusMessage(string message)
        {
            UpdateStatus = message;
        }

        #endregion Message mediator

        #region Commands
        public SimpleCommand<object, object> DoOpenMainWebSiteCommand { get; private set; }
        private void OpenMainWebSite(object args)
        {
            HyperlinkService.OpenLink(this.MainWebSite);
        }
        private bool CanOpenMainWebSite(object args)
        {
            return true;
        }

        public SimpleCommand<object, object> DoOpenDocumentacaoCommand { get; private set; }
        private void OpenDocumentacao(object args)
        {
            HyperlinkService.OpenLink(this.Documentacao);
        }
        private bool CanOpenDocumentacao(object args)
        {
            return true;
        }

        public SimpleCommand<object, object> DoOpenCertificacaoCommand { get; private set; }
        private void OpenCertificacao(object args)
        {
            HyperlinkService.OpenLink(this.Certificacao);
        }
        private bool CanOpenCertificacao(object args)
        {
            return true;
        }

        public SimpleCommand<object, object> DoOpenPortalFinancasCommand { get; private set; }
        private void OpenPortalFinancas(object args)
        {
            HyperlinkService.OpenLink(this.PortalFinancas);
        }
        private bool CanOpenPortalFinancas(object args)
        {
            return true;
        }

        #endregion Commands
    }
}
